﻿using System;
using System.Runtime.Serialization;

namespace DataLayer.Types
{
    [Serializable]
    public class SavingsPlan : BaseRecord<SavingsPlan>, ISerializable
    {
        //need dates to work out term-period
        private DateTime endDate;
        private DateTime startDate;

        private decimal initialAmount;
        private decimal targetAmount;

        private FrequencyItem contributionFrequency;

        //the amount that was actually used from this savings plan
        private decimal retrievedAmount;

        private bool active;

        public SavingsPlan()
        {
        }

        public virtual DateTime StartDate
        {
            get { return startDate; }
            set
            {
                startDate = value;
                OnPropertyChanged("StartDate");
            }
        }

        public virtual DateTime EndDate
        {
            get { return endDate; }
            set
            {
                endDate = value;
                OnPropertyChanged("EndDate");
            }
        }

        public virtual decimal InitialAmount
        {
            get { return initialAmount; }
            set
            {
                initialAmount = value;
                OnPropertyChanged("InitialAmount");
            }
        }

        public virtual decimal TargetAmount
        {
            get { return targetAmount; }
            set
            {
                targetAmount = value;
                OnPropertyChanged("TargetAmount");
            }
        }

        public FrequencyItem ContributionFrequency
        {
            get { return contributionFrequency; }
            set 
            { 
                contributionFrequency = value;
                OnPropertyChanged("ContributionFrequency");
            }
        }

        public virtual bool Active 
        { 
            get { return active; } 
            set{ active = value; OnPropertyChanged("Active");} 
        }

        public virtual decimal RetrievedAmount
        {
            get { return retrievedAmount; }
            set { retrievedAmount = value; OnPropertyChanged("RetrievedAmount");}
        }

            
        #region IBaseRecord Members


        public virtual void Save()
        {
            base.Save(this);
        }

        public virtual void Delete()
        {
            base.Delete(this);
        }

        #endregion

        public virtual void Merge(SavingsPlan savingsPlan)
        {
            StartDate = savingsPlan.StartDate;
            EndDate = savingsPlan.endDate;
            InitialAmount = savingsPlan.initialAmount;
            TargetAmount = savingsPlan.TargetAmount;
            Active = savingsPlan.Active;
            RetrievedAmount = savingsPlan.RetrievedAmount;
        }

        #region ISerializable Members

        public SavingsPlan(SerializationInfo info, StreamingContext context)
        {
            StartDate = info.GetDateTime("StartDate");
            EndDate = info.GetDateTime("EndDate");
            InitialAmount = info.GetDecimal("InitialAmount");
            TargetAmount = info.GetDecimal("TargetAmount");
            Active = info.GetBoolean("Active");
            RetrievedAmount = info.GetDecimal("RetrievedAmount");
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Id", Id);
            info.AddValue("StartDate", StartDate);
            info.AddValue("EndDate", EndDate);
            info.AddValue("InitialAmount", InitialAmount);
            info.AddValue("TargetAmount", TargetAmount);
            info.AddValue("Active", Active);
            info.AddValue("RetrievedAmount", RetrievedAmount);
        }

        #endregion
    }
}